/*
 * Created by JFormDesigner on Sun Jun 21 20:15:27 BST 2009
 */

package gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import resources.Settings;

import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

/**
 * @author Alexandros Ouzounis
 */
public class WindowSettings extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5509550929926484092L;
	public WindowSettings() {
		initComponents();
		loadCurrentSettings();
	}
	
	private void loadCurrentSettings(){
		//general settings
		if (Settings.getInstance().getOpenWindows()){
			checkBoxMultipleWindows.setSelected(true);
		}
		if (Settings.getInstance().getTileOpenWindows()){
			checkBoxTileOpenWindows.setSelected(true);
		}
		if (Settings.getInstance().getTileOpenWindowsResize()){
			checkBoxResizeInternalWindowsOnMainWindowChange.setSelected(true);
		}
		//cpu cores
		sliderCpuCores.setValue(Settings.getInstance().getCpuCores());
		labelCurrentCpuCores.setText("Current Cpu Cores: "+Settings.getInstance().getCpuCores());
	}
	
	private void checkBoxMultipleWindowsStateChanged(ChangeEvent e) {
		if (checkBoxMultipleWindows.getSelectedObjects()!=null){
			Settings.getInstance().updateOpenWindows(true);
		}else Settings.getInstance().updateOpenWindows(false);
		
	}

	private void checkBoxTileOpenWindowsStateChanged(ChangeEvent e) {
		if (checkBoxTileOpenWindows.getSelectedObjects()!=null){
			Settings.getInstance().updateTileOpenWindows(true);
		}else Settings.getInstance().updateTileOpenWindows(false);
		
	}

	private void checkBoxResizeInternalWindowsOnMainWindowChangeStateChanged(ChangeEvent e) {
		if (checkBoxResizeInternalWindowsOnMainWindowChange.getSelectedObjects()!=null){
			Settings.getInstance().updateTileOpenWindowsResize(true);
		}else Settings.getInstance().updateTileOpenWindowsResize(false);
		
	}

	private void buttonRestoreDefaultsStateChanged(ChangeEvent e) {
		checkBoxMultipleWindows.setSelected(true);
		Settings.getInstance().updateOpenWindows(true);
		checkBoxTileOpenWindows.setSelected(true);
		Settings.getInstance().updateTileOpenWindows(true);
		checkBoxResizeInternalWindowsOnMainWindowChange.setSelected(true);
		Settings.getInstance().updateTileOpenWindowsResize(true);
	}

	private void sliderCpuCoresStateChanged(ChangeEvent e) {
		Settings.getInstance().updateCpuCores(sliderCpuCores.getValue());
		labelCurrentCpuCores.setText("Current Cpu Cores: "+Settings.getInstance().getCpuCores());
	}

	private void buttonRestoreDefaults2StateChanged(ChangeEvent e) {
		sliderCpuCores.setValue(Settings.getInstance().getSystemCores());
		labelCurrentCpuCores.setText("Current Cpu Cores: "+Settings.getInstance().getSystemCores());
		Settings.getInstance().updateCpuCores(Settings.getInstance().getSystemCores());
	}

	private void okButtonActionPerformed(ActionEvent e) {
		this.dispose();
	}

	private void cancelButtonActionPerformed(ActionEvent e) {
		this.dispose();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - alex ouzounis
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		tabbedPaneGeneral = new JTabbedPane();
		panelGeneralOptions = new JPanel();
		checkBoxMultipleWindows = new JCheckBox();
		checkBoxTileOpenWindows = new JCheckBox();
		checkBoxResizeInternalWindowsOnMainWindowChange = new JCheckBox();
		vSpacer3 = new JPanel(null);
		panelDefaults = new JPanel();
		buttonRestoreDefaults = new JButton();
		panelCPUOptions = new JPanel();
		labelCpuCores = new JLabel();
		sliderCpuCores = new JSlider();
		sliderCpuCores.setMinimum(1);
		sliderCpuCores.setMaximum(32);
		labelCurrentCpuCores = new JLabel();
		vSpacer1 = new JPanel(null);
		labelCpuCoresDescr = new JLabel();
		vSpacer2 = new JPanel(null);
		panelDefaults2 = new JPanel();
		buttonRestoreDefaults2 = new JButton();
		buttonBar = new JPanel();
		okButton = new JButton();
		cancelButton = new JButton();
		CellConstraints cc = new CellConstraints();

		//======== this ========
		setTitle("Settings");
		setClosable(true);
		setResizable(true);
		setMaximizable(true);
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(Borders.DIALOG_BORDER);

	
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new BorderLayout());

				//======== tabbedPaneGeneral ========
				{

					//======== panelGeneralOptions ========
					{
						panelGeneralOptions.setLayout(new GridLayout(5, 1));

						//---- checkBoxMultipleWindows ----
						checkBoxMultipleWindows.setText("Allow multiple windows open");
						checkBoxMultipleWindows.addChangeListener(new ChangeListener() {
							public void stateChanged(ChangeEvent e) {
								checkBoxMultipleWindowsStateChanged(e);
							}
						});
						panelGeneralOptions.add(checkBoxMultipleWindows);

						//---- checkBoxTileOpenWindows ----
						checkBoxTileOpenWindows.setText("Tile open windows");
						checkBoxTileOpenWindows.addChangeListener(new ChangeListener() {
							public void stateChanged(ChangeEvent e) {
								checkBoxTileOpenWindowsStateChanged(e);
							}
						});
						panelGeneralOptions.add(checkBoxTileOpenWindows);

						//---- checkBoxResizeInternalWindowsOnMainWindowChange ----
						checkBoxResizeInternalWindowsOnMainWindowChange.setText("Resize internal windows on size change of main window");
						checkBoxResizeInternalWindowsOnMainWindowChange.addChangeListener(new ChangeListener() {
							public void stateChanged(ChangeEvent e) {
								checkBoxResizeInternalWindowsOnMainWindowChangeStateChanged(e);
							}
						});
						panelGeneralOptions.add(checkBoxResizeInternalWindowsOnMainWindowChange);
						panelGeneralOptions.add(vSpacer3);

						//======== panelDefaults ========
						{
							panelDefaults.setLayout(null);

							//---- buttonRestoreDefaults ----
							buttonRestoreDefaults.setText("Restore Defaults");
							buttonRestoreDefaults.addChangeListener(new ChangeListener() {
								public void stateChanged(ChangeEvent e) {
									buttonRestoreDefaultsStateChanged(e);
								}
							});
							panelDefaults.add(buttonRestoreDefaults);
							buttonRestoreDefaults.setBounds(0, 0, buttonRestoreDefaults.getPreferredSize().width, 26);

							{ // compute preferred size
								Dimension preferredSize = new Dimension();
								for(int i = 0; i < panelDefaults.getComponentCount(); i++) {
									Rectangle bounds = panelDefaults.getComponent(i).getBounds();
									preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
									preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
								}
								Insets insets = panelDefaults.getInsets();
								preferredSize.width += insets.right;
								preferredSize.height += insets.bottom;
								panelDefaults.setMinimumSize(preferredSize);
								panelDefaults.setPreferredSize(preferredSize);
							}
						}
						panelGeneralOptions.add(panelDefaults);
					}
					tabbedPaneGeneral.addTab("General", panelGeneralOptions);


					//======== panelCPUOptions ========
					{
						panelCPUOptions.setLayout(new GridLayout(7, 1));

						//---- labelCpuCores ----
						labelCpuCores.setText("Number of cpu cores:");
						panelCPUOptions.add(labelCpuCores);

						//---- sliderCpuCores ----
						sliderCpuCores.addChangeListener(new ChangeListener() {
							public void stateChanged(ChangeEvent e) {
								sliderCpuCoresStateChanged(e);
							}
						});
						panelCPUOptions.add(sliderCpuCores);

						//---- labelCurrentCpuCores ----
						labelCurrentCpuCores.setText("Default Setting : 2");
						panelCPUOptions.add(labelCurrentCpuCores);
						panelCPUOptions.add(vSpacer1);

						//---- labelCpuCoresDescr ----
						labelCpuCoresDescr.setText("Change in cpu settings will take effect upon the next new mining. ");
						panelCPUOptions.add(labelCpuCoresDescr);
						panelCPUOptions.add(vSpacer2);

						//======== panelDefaults2 ========
						{
							panelDefaults2.setLayout(null);

							//---- buttonRestoreDefaults2 ----
							buttonRestoreDefaults2.setText("Restore Defaults");
							buttonRestoreDefaults2.addChangeListener(new ChangeListener() {
								public void stateChanged(ChangeEvent e) {
									buttonRestoreDefaults2StateChanged(e);
								}
							});
							panelDefaults2.add(buttonRestoreDefaults2);
							buttonRestoreDefaults2.setBounds(0, 0, buttonRestoreDefaults2.getPreferredSize().width, 19);

							{ // compute preferred size
								Dimension preferredSize = new Dimension();
								for(int i = 0; i < panelDefaults2.getComponentCount(); i++) {
									Rectangle bounds = panelDefaults2.getComponent(i).getBounds();
									preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
									preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
								}
								Insets insets = panelDefaults2.getInsets();
								preferredSize.width += insets.right;
								preferredSize.height += insets.bottom;
								panelDefaults2.setMinimumSize(preferredSize);
								panelDefaults2.setPreferredSize(preferredSize);
							}
						}
						panelCPUOptions.add(panelDefaults2);
					}
					tabbedPaneGeneral.addTab("CPU Settings", panelCPUOptions);

				}
				contentPanel.add(tabbedPaneGeneral, BorderLayout.CENTER);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
				buttonBar.setLayout(new FormLayout(
					"$glue, $button, $rgap, $button",
					"pref"));

				//---- okButton ----
				okButton.setText("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						okButtonActionPerformed(e);
					}
				});
				buttonBar.add(okButton, cc.xy(2, 1));

				//---- cancelButton ----
				cancelButton.setText("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cancelButtonActionPerformed(e);
					}
				});
				buttonBar.add(cancelButton, cc.xy(4, 1));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - alex ouzounis
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JTabbedPane tabbedPaneGeneral;
	private JPanel panelGeneralOptions;
	private JCheckBox checkBoxMultipleWindows;
	private JCheckBox checkBoxTileOpenWindows;
	private JCheckBox checkBoxResizeInternalWindowsOnMainWindowChange;
	private JPanel vSpacer3;
	private JPanel panelDefaults;
	private JButton buttonRestoreDefaults;
	private JPanel panelCPUOptions;
	private JLabel labelCpuCores;
	private JSlider sliderCpuCores;
	private JLabel labelCurrentCpuCores;
	private JPanel vSpacer1;
	private JLabel labelCpuCoresDescr;
	private JPanel vSpacer2;
	private JPanel panelDefaults2;
	private JButton buttonRestoreDefaults2;
	private JPanel buttonBar;
	private JButton okButton;
	private JButton cancelButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
